import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/manager/xindong_yinsi_singleton.dart';
import 'package:xindong/routes/user/noble/xindong_noble_ctr.dart';
import 'package:xindong/routes/user/noble/xindong_noble_list_view.dart';
import 'package:xindong/routes/user/noble/xindong_noble_privilege_grid_view.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/xindong_avatar.dart';

class XinDongNobleYeMian extends StatefulWidget {
  const XinDongNobleYeMian({super.key});

  @override
  State<XinDongNobleYeMian> createState() => _XinDongNobleYeMianState();
}

class _XinDongNobleYeMianState extends State<XinDongNobleYeMian> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("黑卡会员"),
      body: ListView(
        physics: const ClampingScrollPhysics(),
        children: [
          Container(
            height: 80,
            padding: const EdgeInsets.only(left: 16, right: 16),
            decoration: BoxDecoration(
              // 渐变色
              gradient: LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [DZColors.hexStringColor("C394FC"), DZColors.hexStringColor("FBD0FC")],
              ),
            ),
            child: GetBuilder<XinDongUserController>(builder: (controller) {
              String text = "";
              if (controller.userInfo?.is_noble == 1) {
                text = "您目前是黑卡会员${controller.userInfo?.noble_grade}\n有效期至：${controller.userInfo?.noble_time}";
              } else {
                text = "未开通会员";
              }
              return Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  XinDongAvatarBorder(controller.userInfo, avatarSize: 46),
                  const SizedBox(width: 10),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "${controller.userInfo?.getName}",
                        style: const TextStyle(color: DZColors.white, fontSize: 16),
                      ),
                      Text(
                        text,
                        style: const TextStyle(color: Colors.white70, fontSize: 13),
                      ),
                    ],
                  ),
                ],
              );
            }),
          ),
          Container(
            padding: const EdgeInsets.all(16),
            decoration: const BoxDecoration(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(16), topRight: Radius.circular(16)),
            ),
            child: const XinDongNobleListView(),
          ),
          buildXieYi(),
          Container(
            height: kButtonHeight,
            margin: const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16),
            child: XinDongCommonButton(
              '立即开通',
              onTap: () => XinDongNobleCtr.to.onTapBuy(),
            ),
          ),
          const Padding(
            padding: EdgeInsets.only(left: 16.0, right: 16.0),
            child: Text(
              "温馨提示：如您已支付成功，请重新进来当前页面，平台可能稍有延误！",
              style: TextStyle(color: DZColors.contentColor, fontSize: 14),
            ),
          ),
          const XinDongNoblePrivilegeGridView(),
          SizedBox(height: bottomBarHeight + 20),
        ],
      ),
    );
  }

  buildXieYi() {
    return GestureDetector(
      onTap: () => XinDongYinSiSingleton.to.changeHuiYuan(),
      behavior: HitTestBehavior.opaque,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GetBuilder<XinDongYinSiSingleton>(
            builder: (controller) {
              return Checkbox(
                value: controller.agreement.agreeHuiYuan,
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                onChanged: (value) => controller.changeHuiYuan(),
              );
            },
          ),
          Flexible(
            child: Text.rich(
              TextSpan(text: "您已阅读并同意", children: [
                TextSpan(
                  text: "《会员服务协议》",
                  style: const TextStyle(color: DZColors.mainBlueColor),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      XinDongPageSingleton.instance.jumpToWebYeMian(title: "会员服务协议", url: XinDongNetworking.instance.huiYuanUrl);
                    },
                ),
              ]),
              style: const TextStyle(fontSize: 14, color: DZColors.contentColor, height: 1.2),
            ),
          ),
        ],
      ),
    );
  }
}
